<?php
namespace control;
/*
*现金流量表类
*/

class cashFlowClass extends adIndexClass
{
    private const page = 'cashFlow';
    private const title = '现金流量表';
    private const table = 'cash';
    public bool $isExport = false;

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    "会计期间" => text(array("name" => "month", 'laydate' => ['type' => 'month'], 'value' => date('Y-m'))),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*列表-查询条件*/
    public function home()
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        if ($this->power(self::page, "export")) {
            $button .= "  <a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=exportData'><span class='spanButton'>导出</span></a>\n";
        }
        if ($this->power(self::page, "print")) {
            $button .= "<span class='spanButton'  onclick=\"window.open('" . root . "control/adIndex.html?cla=" . self::page . "&fun=printPage')\">打印</span>";
        }
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        $month = $this->post('month');
        static::rSet(self::$stid . self::page . 'Month', $month);
        // 月份选择器
        $monthSelector = componentClass::monthSelector(implode('~',[$month,$month]));
        $tr[] = array('项目', '行次', "本年累计金额", '本期金额'.$monthSelector['button']);
        $tr = array_merge($tr, $this->content($month));

        $assetObj = new assetDebtClass();
        //返回
        $json['html'] =
            "<div class='acct-main-container'>
                <form name='listForm'>" .
                $this->tableMany(array("tr" => $tr,"displayFields" => false)) .
            "</form>" .
            $monthSelector['html'].
            "</div>" .
            $this->css() .
            $assetObj->tableCss() .
            clear;
        return json_encode($json, 320);
    }

    private function css()
    {
        return <<<eof
<style>
.tableMany{
  table-layout: fixed;
}
</style>
eof;

    }

    /**
     * 获取内容
     * @param $month
     * @return array
     */
    private function content($month): array
    {
        $assetObj = new assetDebtClass();
        $accountRule = companyClass::returnRule(1);
        $all = find(array('table' => self::table, 'field' => "distinct type", 'where' => "coid = '" . self::$coid . "' and type != '' order by any_value(code)"));
        $list = 0;
        $bigNum = ['一、', '二、', '三、','四、'];
        $cashAdd = [
            'balance' => 0,
            'monthBalance' => 0,
        ];
        $total = [
            '经营活动现金流入小计'=>[
                'balance'=>0,
                'monthBalance'=>0
            ],
            '经营活动现金流出小计'=>[
                'balance'=>0,
                'monthBalance'=>0
            ],
            '投资活动现金流入小计'=>[
                'balance'=>0,
                'monthBalance'=>0
            ],
            '投资活动现金流出小计'=>[
                'balance'=>0,
                'monthBalance'=>0
            ],
            '筹资活动现金流入小计'=>[
                'balance'=>0,
                'monthBalance'=>0
            ],
            '筹资活动现金流出小计'=>[
                'balance'=>0,
                'monthBalance'=>0
            ],
        ];
        $tr=[];
        if ($accountRule =='enterpriseRule'){
            foreach ($all['array'] as $index => $array) {

                if ($array['type'] =='汇率变动对现金的影响额'){
                    $cash = query(self::table,"coid = '".self::$coid."' and type = '$array[type]'");
                    $result = $this->balance($month, $cash['id']);
                    $tr[] = ["<span style='font-weight: bold'>{$bigNum[$index]}{$cash['name']}</span>", ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                    continue;
                }else{
                    $tr[] = ["<span style='font-weight: bold'>{$bigNum[$index]}{$array['type']}</span>", ' ', ' ', ' '];
                }
                $children = find(array('table' => self::table, 'where' => "coid = '" . self::$coid . "' and type = '$array[type]' order by code"));
                foreach ($children['array'] as $item) {
                    $result = $this->balance($month, $item['id']);
                    if ($item['type'] == '经营活动产生的现金流量' && $item['direction']=='+'){
                        $total['经营活动现金流入小计']['balance'] += $result['balance'];
                        $total['经营活动现金流入小计']['monthBalance'] += $result['monthBalance'];
                    }
                    if ($item['type'] == '经营活动产生的现金流量' && $item['direction']=='-'){
                        $total['经营活动现金流出小计']['balance'] += $result['balance'];
                        $total['经营活动现金流出小计']['monthBalance'] += $result['monthBalance'];
                    }
                    if ($item['type'] == '投资活动产生的现金流量' && $item['direction']=='+'){
                        $total['投资活动现金流入小计']['balance'] += $result['balance'];
                        $total['投资活动现金流入小计']['monthBalance'] += $result['monthBalance'];
                    }
                    if ($item['type'] == '投资活动产生的现金流量' && $item['direction']=='-'){
                        $total['投资活动现金流出小计']['balance'] += $result['balance'];
                        $total['投资活动现金流出小计']['monthBalance'] += $result['monthBalance'];
                    }
                    if ($item['type'] == '筹资活动产生的现金流量' && $item['direction']=='+'){
                        $total['筹资活动现金流入小计']['balance'] += $result['balance'];
                        $total['筹资活动现金流入小计']['monthBalance'] += $result['monthBalance'];
                    }
                    if ($item['type'] == '筹资活动产生的现金流量' && $item['direction']=='-'){
                        $total['筹资活动现金流出小计']['balance'] += $result['balance'];
                        $total['筹资活动现金流出小计']['monthBalance'] += $result['monthBalance'];
                    }
                    $tr[] = [$item['name'], ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                    if ($item['name'] =='收到其他与经营活动有关的现金'){
                        $tr[] = ['经营活动现金流入小计', ++$list, $assetObj->fontColor($total['经营活动现金流入小计']['balance']), $assetObj->fontColor($total['经营活动现金流入小计']['monthBalance'])];
                    }
                    if ($item['name'] =='支付其他与经营活动有关的现金'){
                        $tr[] = ['经营活动现金流出小计', ++$list, $assetObj->fontColor($total['经营活动现金流出小计']['balance']), $assetObj->fontColor($total['经营活动现金流出小计']['monthBalance'])];
                    }
                    if ($item['name'] =='收到其他与投资活动有关的现金'){
                        $tr[] = ['投资活动现金流入小计', ++$list, $assetObj->fontColor($total['投资活动现金流入小计']['balance']), $assetObj->fontColor($total['投资活动现金流入小计']['monthBalance'])];
                    }
                    if ($item['name'] =='支付其他与投资活动有关的现金'){
                        $tr[] = ['投资活动现金流出小计', ++$list, $assetObj->fontColor($total['投资活动现金流出小计']['balance']), $assetObj->fontColor($total['投资活动现金流出小计']['monthBalance'])];
                    }
                    if ($item['name'] =='收到其他与筹资活动有关的现金'){
                        $tr[] = ['筹资活动现金流入小计', ++$list, $assetObj->fontColor($total['筹资活动现金流入小计']['balance']), $assetObj->fontColor($total['筹资活动现金流入小计']['monthBalance'])];
                    }
                    if ($item['name'] =='支付其他与筹资活动有关的现金'){
                        $tr[] = ['筹资活动现金流出小计', ++$list, $assetObj->fontColor($total['筹资活动现金流出小计']['balance']), $assetObj->fontColor($total['筹资活动现金流出小计']['monthBalance'])];
                    }
                }
                $result = $this->totalBalance($array['type'], $month);
                $cashAdd['balance'] += $result['balance'];
                $cashAdd['monthBalance'] += $result['monthBalance'];
                $tr[] = [$array['type'] . '净额', ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
            }
            $tr[] = ["<span style='font-weight: bold'>五、现金及现金等价物净增加额</span>", ++$list, $assetObj->fontColor($cashAdd['balance']), $assetObj->fontColor($cashAdd['monthBalance'])];
            $result = $this->initPeriodBalance($month);
            $tr[] = ["&emsp;加：期初现金及现金等价物的余额", ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
            $tr[] = ["<span style='font-weight: bold'>六、期末现金及现金等价物余额</span>", ++$list, $assetObj->fontColor($cashAdd['balance'] + $result['balance']), $assetObj->fontColor($cashAdd['monthBalance'] + $result['monthBalance'])];
        }elseif($accountRule =='smallEnterpriseRule'){
            foreach ($all['array'] as $index => $array) {
                $tr[] = ["<span style='font-weight: bold'>{$bigNum[$index]}{$array['type']}</span>", ' ', ' ', ' '];
                $children = find(array('table' => self::table, 'where' => "coid = '" . self::$coid . "' and type = '$array[type]' order by code"));
                foreach ($children['array'] as $item) {
                    $result = $this->balance($month, $item['id']);
                    $tr[] = [$item['name'], ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                }
                $result = $this->totalBalance($array['type'], $month);
                $cashAdd['balance'] += $result['balance'];
                $cashAdd['monthBalance'] += $result['monthBalance'];
                $tr[] = [$array['type'] . '净额', ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
            }
            $tr[] = ["<span style='font-weight: bold'>四、现金净增加额</span>", ++$list, $assetObj->fontColor($cashAdd['balance']), $assetObj->fontColor($cashAdd['monthBalance'])];
            $result = $this->initPeriodBalance($month);
            $tr[] = ["&emsp;加：期初现金金额", ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
            $tr[] = ["<span style='font-weight: bold'>五、期末现金金额</span>", ++$list, $assetObj->fontColor($cashAdd['balance'] + $result['balance']), $assetObj->fontColor($cashAdd['monthBalance'] + $result['monthBalance'])];
        }

        return $tr;
    }

    /**
     * 期初金额
     */
    public function initPeriodBalance($month): array
    {
        $year = date('Y', strtotime($month . '-01'));
        $company = query('company', "coid = '" . self::$coid . "'");
        $companyYear = date('Y', strtotime($company['startDate'] . '-01'));

        //本期期初
        $monthBalance = find(array('table' => 'subject', 'where' => "coid = '" . self::$coid . "' and assistAccountIds like '%现金流%' and assistAccount = '是'", 'sum' => "initMoney"));
        $monthBalance = self::centToYuan($monthBalance);
        //本年累计期初
        $initMoney = 0;
        $all = find(array('table' => 'subject', 'where' => "coid = '" . self::$coid . "' and assistAccountIds like '%现金流%' and assistAccount = '是'"));
        foreach ($all['array'] as $array) {
            if ($array['direction'] == '借') {
                $initMoney += $array['loanMoney'] + $array['initMoney'] - $array['borrowMoney'];
            } else {
                $initMoney += $array['initMoney'] + $array['borrowMoney'] - $array['loanMoney'];
            }
        }
        $initMoney = self::centToYuan($initMoney);
        if ($year != $companyYear) {
            $all = find(array('table' => self::table, 'field' => "distinct type", 'where' => "coid = '" . self::$coid . "' and type != '' order by any_value(code)"));
            foreach ($all['array'] as $array) {
                $result = $this->totalBalance($array['type'], $month, '期初');
                $initMoney += $result['balance'];
            }
        }
        $balance = $initMoney;
        if ($month != $company['startDate']) {
            $all = find(array('table' => self::table, 'field' => "distinct type", 'where' => "coid = '" . self::$coid . "' and type != '' order by any_value(code)"));
            foreach ($all['array'] as $array) {
                $result = $this->totalBalance($array['type'], $month, '本期期初');
                $monthBalance+=$result['balance'];
            }
        }
        return ['balance' => $balance, 'monthBalance' => $monthBalance];
    }

    /**
     * 净额
     */
    public function totalBalance($type, $month, $source = ''): array
    {
        $all = find(array('table' => self::table, 'where' => "type = '$type' and coid = '" . self::$coid . "' order by code"));
        $balance = $monthBalance = 0;
        foreach ($all['array'] as $array) {
            if ($array['direction'] == '+') {
                $balance += $this->yearMoney($month, $array['id'], $source);
            } else {
                $balance -= $this->yearMoney($month, $array['id'], $source);
            }
            if ($array['direction'] == '+') {
                $monthBalance += $this->monthMoney($month, $array['id']);
            } else {
                $monthBalance -= $this->monthMoney($month, $array['id']);
            }
        }
        return ['balance' => $balance, 'monthBalance' => $monthBalance];
    }

    /**
     * 期末余额
     */
    public function balance($month, $targetId)
    {
        $balance = $this->yearMoney($month, $targetId);
        $monthBalance = $this->monthMoney($month, $targetId);
        return ['balance' => $balance, 'monthBalance' => $monthBalance];
    }

    /**
     * 本年累计
     */
    public function yearMoney($month, $targetId, $source = ''): float|int
    {
        $company = query('company', "coid = '" . self::$coid . "'");
        $year = date('Y', strtotime($month . '-01'));
        $cash = query('cash', "id = '$targetId'");
        if ($source == '期初') {
            //计算本年累计期初使用
            $where = " and date_format(a.recordDate,'%Y') < '$year' and b.cashId = '$targetId' ";
        } elseif ($source == '本期期初') {
            $where = " and date_format(a.recordDate,'%Y-%m') < '$month' and b.cashId = '$targetId' ";
        } else {
            $where = " and date_format(a.recordDate,'%Y') = '$year' and date_format(a.recordDate,'%Y-%m') <= '$month' and b.cashId = '$targetId' ";
        }
        $jieInit = find(array("table" => "voucher as a join voucher_detail as b on a.id = b.orderId", 'field' => "ifnull(sum(b.money),0) as money", "single" => "a.isTemplate = '否' and a.coid = '" . self::$coid . "' and b.direction = '借'" . $where.voucherClass::joinWhere()));
        $daiInit = find(array("table" => "voucher as a join voucher_detail as b on a.id = b.orderId", 'field' => "ifnull(sum(b.money),0) as money", "single" => "a.isTemplate = '否' and a.coid = '" . self::$coid . "' and b.direction = '贷'" . $where.voucherClass::joinWhere()));
        $jieInit['money'] = self::centToYuan($jieInit['money']);
        $daiInit['money'] = self::centToYuan($daiInit['money']);
        if ($cash['direction'] == '+') {
            $balance = $jieInit['money'] - $daiInit['money'];
        } else {
            $balance = $daiInit['money'] - $jieInit['money'];
        }
        if (empty($source)) {
            if ($year <= date('Y', strtotime($company['startDate'] . '-01'))) {
                $borrowMoney = find(array('table' => 'subject_assist', 'where' => "cashId = '$cash[id]' and coid = '" . self::$coid . "'", 'sum' => "jieMoney"));
                $loanMoney = find(array('table' => 'subject_assist', 'where' => "cashId = '$cash[id]' and coid = '" . self::$coid . "'", 'sum' => "daiMoney"));
                if ($cash['direction'] == '+') {
                    $balance += $borrowMoney-$loanMoney;
                } else {
                    $balance += $loanMoney-$borrowMoney;
                }
            }
        }
        return $balance;
    }


    /**
     * 本期金额
     */
    public function monthMoney($month, $targetId): float|int
    {
        $cash = query('cash', "id = '$targetId'");
        $where = " and date_format(a.recordDate,'%Y-%m') = '$month' and b.cashId = '$targetId'";
        $jieInit = find(array("table" => "voucher as a join voucher_detail as b on a.id = b.orderId", 'field' => "ifnull(sum(b.money),0) as money", "single" => "a.isTemplate = '否' and a.coid = '" . self::$coid . "' and b.direction = '借'" . $where.voucherClass::joinWhere()));
        $daiInit = find(array("table" => "voucher as a join voucher_detail as b on a.id = b.orderId", 'field' => "ifnull(sum(b.money),0) as money", "single" => "a.isTemplate = '否' and a.coid = '" . self::$coid . "' and b.direction = '贷'" . $where.voucherClass::joinWhere()));
        $jieInit['money'] = self::centToYuan($jieInit['money']);
        $daiInit['money'] = self::centToYuan($daiInit['money']);
        if ($cash['direction'] == '+') {
            $balance = $jieInit['money'] - $daiInit['money'];
        } else {
            $balance = $daiInit['money'] - $jieInit['money'];
        }
        return $balance;
    }

    public function printPage()
    {
        $month = static::rGet(self::$stid . self::page . 'Month');
        $tr = $this->content($month);
        $company = query('company', "coid = '" . self::$coid . "'");
        $tempHtml = '';
        foreach ($tr as $array) {
            $tempHtml .= "<tr class='have_border'>";
            for ($i = 0; $i <= 3; $i++) {
                $tempHtml .= "<td>$array[$i]</td>";
            }
            $tempHtml .= "</tr>";
        }

        $divHtml = "
                 <div>
                      <div class='printTitle' style='font-weight: bold'>现金流量表</div>
                 </div>
                 <div class='table-top-title'><div style='flex: 1'>编制单位：$company[name]</div><div style='flex: 1;text-align: center'>$month</div><div style='flex: 1;text-align: right'>单位：元</div></div>
              <table class='printTable' style='font-size: 13px'  data-op-type='block-box'>
                <tbody class='nop-fill-box'>
               <tr class='have_border'>
                   <td>项目</td>
                   <td>行次</td>
                   <td>本年累计金额</td>
                   <td>本期金额</td>
                </tr>
                $tempHtml
               </tbody>
              </table>
            ";
        $html = <<<eof
                    <div id="content-box" style="display: none">
                        <div data-op-type='new-page'></div>
                    <!--    <div data-op-type="pendants">&lt;!&ndash; 定义页面部件（页眉/页脚/书签/水印背景等） &ndash;&gt;-->
                    <!--        <div class='pendant-title'>实发工资表</div>-->
                    <!--    </div>-->
                       $divHtml
                    </div>
                  <style>
                      .footer-ul-parent{float: right;margin-right: 10px}
                      .footer-ul li{margin-top: 10px}
                      .table-top-title{
                        height: 30px;
                        line-height: 30px;
                        font-size: 14px;
                        margin-top: 20px;
                        display: flex;
                        flex-direction: row;
                        justify-content: space-between;
                      }
</style>
eof;
        $print = new printClass();

        return $print->showPrint($html, 'portrait', ['padding' => "5mm 10mm 5mm 10mm"]);
    }

    /**
     * 导出
     */
    public function exportData()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $tr = [];
            $accountRule = companyClass::returnRule(1);
            $assetObj = new assetDebtClass();
            $assetObj->isExport = true;
            $month = static::rGet(self::$stid . self::page . 'Month');
            $title = array('项目', '行次', "本年累计金额", '本期金额');

            $all = find(array('table' => self::table, 'field' => "distinct type", 'where' => "coid = '" . self::$coid . "' and type != '' order by any_value(code)"));
            $list = 0;
            $bigNum = ['一、', '二、', '三、', '四、'];
            $cashAdd = [
                'balance' => 0,
                'monthBalance' => 0,
            ];
            $total = [
                '经营活动现金流入小计'=>[
                    'balance'=>0,
                    'monthBalance'=>0
                ],
                '经营活动现金流出小计'=>[
                    'balance'=>0,
                    'monthBalance'=>0
                ],
                '投资活动现金流入小计'=>[
                    'balance'=>0,
                    'monthBalance'=>0
                ],
                '投资活动现金流出小计'=>[
                    'balance'=>0,
                    'monthBalance'=>0
                ],
                '筹资活动现金流入小计'=>[
                    'balance'=>0,
                    'monthBalance'=>0
                ],
                '筹资活动现金流出小计'=>[
                    'balance'=>0,
                    'monthBalance'=>0
                ],
            ];
            if ($accountRule =='enterpriseRule'){
                foreach ($all['array'] as $index => $array) {
                    if ($array['type'] =='汇率变动对现金的影响额'){
                        $cash = query(self::table,"coid = '".self::$coid."' and type = '$array[type]'");
                        $result = $this->balance($month, $cash['id']);
                        $tr[] = [$bigNum[$index].$cash['name'], ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                        continue;
                    }else{
                        $tr[] = [$bigNum[$index].$array['type'], ' ', ' ', ' '];
                    }
                    $children = find(array('table' => self::table, 'where' => "coid = '" . self::$coid . "' and type = '$array[type]' order by code"));
                    foreach ($children['array'] as $item) {
                        $result = $this->balance($month, $item['id']);
                        if ($item['type'] == '经营活动产生的现金流量' && $item['direction']=='+'){
                            $total['经营活动现金流入小计']['balance'] += $result['balance'];
                            $total['经营活动现金流入小计']['monthBalance'] += $result['monthBalance'];
                        }
                        if ($item['type'] == '经营活动产生的现金流量' && $item['direction']=='-'){
                            $total['经营活动现金流出小计']['balance'] += $result['balance'];
                            $total['经营活动现金流出小计']['monthBalance'] += $result['monthBalance'];
                        }
                        if ($item['type'] == '投资活动产生的现金流量' && $item['direction']=='+'){
                            $total['投资活动现金流入小计']['balance'] += $result['balance'];
                            $total['投资活动现金流入小计']['monthBalance'] += $result['monthBalance'];
                        }
                        if ($item['type'] == '投资活动产生的现金流量' && $item['direction']=='-'){
                            $total['投资活动现金流出小计']['balance'] += $result['balance'];
                            $total['投资活动现金流出小计']['monthBalance'] += $result['monthBalance'];
                        }
                        if ($item['type'] == '筹资活动产生的现金流量' && $item['direction']=='+'){
                            $total['筹资活动现金流入小计']['balance'] += $result['balance'];
                            $total['筹资活动现金流入小计']['monthBalance'] += $result['monthBalance'];
                        }
                        if ($item['type'] == '筹资活动产生的现金流量' && $item['direction']=='-'){
                            $total['筹资活动现金流出小计']['balance'] += $result['balance'];
                            $total['筹资活动现金流出小计']['monthBalance'] += $result['monthBalance'];
                        }
                        $tr[] = [$item['name'], ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                        if ($item['name'] =='收到其他与经营活动有关的现金'){
                            $tr[] = ['经营活动现金流入小计', ++$list, $assetObj->fontColor($total['经营活动现金流入小计']['balance']), $assetObj->fontColor($total['经营活动现金流入小计']['monthBalance'])];
                        }
                        if ($item['name'] =='支付其他与经营活动有关的现金'){
                            $tr[] = ['经营活动现金流出小计', ++$list, $assetObj->fontColor($total['经营活动现金流出小计']['balance']), $assetObj->fontColor($total['经营活动现金流出小计']['monthBalance'])];
                        }
                        if ($item['name'] =='收到其他与投资活动有关的现金'){
                            $tr[] = ['投资活动现金流入小计', ++$list, $assetObj->fontColor($total['投资活动现金流入小计']['balance']), $assetObj->fontColor($total['投资活动现金流入小计']['monthBalance'])];
                        }
                        if ($item['name'] =='支付其他与投资活动有关的现金'){
                            $tr[] = ['投资活动现金流出小计', ++$list, $assetObj->fontColor($total['投资活动现金流出小计']['balance']), $assetObj->fontColor($total['投资活动现金流出小计']['monthBalance'])];
                        }
                        if ($item['name'] =='收到其他与筹资活动有关的现金'){
                            $tr[] = ['筹资活动现金流入小计', ++$list, $assetObj->fontColor($total['筹资活动现金流入小计']['balance']), $assetObj->fontColor($total['筹资活动现金流入小计']['monthBalance'])];
                        }
                        if ($item['name'] =='支付其他与筹资活动有关的现金'){
                            $tr[] = ['筹资活动现金流出小计', ++$list, $assetObj->fontColor($total['筹资活动现金流出小计']['balance']), $assetObj->fontColor($total['筹资活动现金流出小计']['monthBalance'])];
                        }
                    }
                    $result = $this->totalBalance($array['type'], $month);
                    $cashAdd['balance'] += $result['balance'];
                    $cashAdd['monthBalance'] += $result['monthBalance'];
                    $tr[] = [$array['type'] . '净额', ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                }
                $tr[] = ["五、现金及现金等价物净增加额", ++$list, $assetObj->fontColor($cashAdd['balance']), $assetObj->fontColor($cashAdd['monthBalance'])];
                $result = $this->initPeriodBalance($month);
                $tr[] = ["&emsp;加：期初现金及现金等价物的余额", ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                $tr[] = ["六、期末现金及现金等价物余额", ++$list, $assetObj->fontColor($cashAdd['balance'] + $result['balance']), $assetObj->fontColor($cashAdd['monthBalance'] + $result['monthBalance'])];
            }elseif($accountRule =='smallEnterpriseRule'){
                foreach ($all['array'] as $index => $array) {
                    $tr[] = [$bigNum[$index] . $array['type'], ' ', ' ', ' '];
                    $children = find(array('table' => self::table, 'where' => "coid = '" . self::$coid . "' and type = '$array[type]' order by code"));
                    foreach ($children['array'] as $item) {
                        $result = $this->balance($month, $item['id']);
                        $tr[] = [$item['name'], ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                    }
                    $result = $this->totalBalance($array['type'], $month);
                    $cashAdd['balance'] += $result['balance'];
                    $cashAdd['monthBalance'] += $result['monthBalance'];
                    $tr[] = [$array['type'] . '净额', ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                }
                $tr[] = ["四、现金净增加额", ++$list, $assetObj->fontColor($cashAdd['balance']), $assetObj->fontColor($cashAdd['monthBalance'])];

                $result = $this->initPeriodBalance($month);
                $tr[] = ["加：期初现金金额", ++$list, $assetObj->fontColor($result['balance']), $assetObj->fontColor($result['monthBalance'])];
                $tr[] = ["五、期末现金金额", ++$list, $assetObj->fontColor($cashAdd['balance'] + $result['balance']), $assetObj->fontColor($cashAdd['monthBalance'] + $result['monthBalance'])];

            }

            $text = $this->control['name'] . "导出了现金流量表";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $tr, "现金流量表.xlsx");
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }

}

